trigger IYPPRINTValidation on OpportunityLineItem(before insert,before update)
{
Set<Id> setoppid=new Set<Id>();
ID RecTypeID = RecordTypeHelper.GetRecordTypeID('Opportunity','Contract');
for(OpportunityLineItem oli:trigger.new)
{
setoppid.add(oli.OpportunityId);
}
Map<Id,PricebookEntry> mapPricebookEntry=new Map<id,PricebookEntry>([Select ProductCode,Product2Id,Product2.Name,Name From PricebookEntry]);
Map<Id,List<OpportunityLineItem>> mapOLI=new Map<Id,List<OpportunityLineItem>>();

List<Opportunity> lstopportunity=new List<Opportunity>();
for(Opportunity opp:[Select Approval_Discount__c,Approval_Product__c,Approval_Received__c,Amount,(Select OpportunityId,PricebookEntryId From OpportunityLineItems where id not in:trigger.new) From Opportunity where Id in:setoppid and RecordTypeId=:RecTypeID])
{
    List<OpportunityLineItem> lstOpportunityLineItem=opp.OpportunityLineItems;
     mapOLI.put(opp.Id,lstOpportunityLineItem);
}
for(OpportunityLineItem oli:trigger.new)
{
    string productname=mapPricebookEntry.get(oli.PricebookEntryId).Product2.Name;
    if(productname=='IYP')
    {
        List<OpportunityLineItem> lstoli=mapOLI.get(oli.OpportunityId);
        for(OpportunityLineItem existoli:lstoli)
        {
            string prdname=mapPricebookEntry.get(existoli.PricebookEntryId).Product2.Name;
            if(prdname=='PRINT')
            {
                oli.addError('Can not add IYP product because PRINT product is already there');
            }
        }

    }
    else if(productname=='PRINT')
    {
        List<OpportunityLineItem> lstoli=mapOLI.get(oli.OpportunityId);
        for(OpportunityLineItem existoli:lstoli)
        {
            string prdname=mapPricebookEntry.get(existoli.PricebookEntryId).Product2.Name;
            if(prdname=='IYP')
            {
                oli.addError('Can not add PRINT product because IYP product is already there');
            }
        }
    }
}
}